﻿Public Class FormCompteFichiers

    Private Sub ButtonParcourir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonParcourir.Click
        If Me.FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.TextBoxDossiers.Text &= Me.FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub ButtonCompter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCompter.Click
        Me.Cursor = Cursors.WaitCursor
        Me.ProgressBar1.Visible = True
        Me.TextBoxResult.AppendText(Commun.CompterFichiers(TextBoxRef.Text, ComboBoxRegex.Text, TextBoxChaineRecherchee.Text, TextBoxRapport.Text, TextBoxDossiers.Text, TextBoxMasque.Text, CheckBoxRecursion.Checked))
        Me.ProgressBar1.Visible = False
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub ButtonAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAnnuler.Click
        Me.Close()
    End Sub

    Private Sub ButtonCopier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCopier.Click
        My.Computer.Clipboard.Clear()
        My.Computer.Clipboard.SetText(Me.TextBoxResult.Text)
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call Form1.Nouveau()
        Form1.RichTextBox1.AppendText(Me.TextBoxResult.Text)
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.TextBoxRef.Text = Form1.RichTextBox1.Text.Replace(vbLf, vbCrLf)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.OpenFileDialog1.Multiselect = False
        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each sFichier As String In Me.OpenFileDialog1.FileNames()
                Try
                    Me.TextBoxRef.SelectedText = My.Computer.FileSystem.ReadAllText(sFichier, System.Text.Encoding.Default) & vbCrLf
                    Me.TextBoxRef.SelectionStart = Me.TextBoxRef.SelectionStart + Me.TextBoxRef.SelectionLength
                Catch Ex As Exception
                    MsgBox("Erreur à la lecture du fichier " & sFichier & ": " & Ex.Message, MsgBoxStyle.Exclamation)
                End Try
            Next
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.TextBoxResult.Text = ""
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.TextBoxRef.Text = ""
    End Sub

    Private Sub FormCompteFichiers_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Me.ComboBoxRegex.Items.Clear()
        'Options par défaut
        Me.ComboBoxRegex.Items.Add(".*")
        'Marqueurs
        Dim oMarqueur As ClassMarqueur
        For Each oItem In Form1.ListViewMarqueurs.Items
            oMarqueur = oItem.Tag
            Me.ComboBoxRegex.Items.Add(oMarqueur.Masque)
        Next
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        FormPreview.RichTextBox1.Text = ""
        For Each oMatch As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(Me.TextBoxRef.Text, Me.ComboBoxRegex.Text, System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
            Dim sRecherche As String = Me.TextBoxChaineRecherchee.Text
            For i = 0 To oMatch.Groups.Count - 1
                sRecherche = sRecherche.Replace("$" & i, oMatch.Groups(i).Value)
            Next
            FormPreview.RichTextBox1.AppendText(sRecherche & vbCrLf)
        Next
        FormPreview.ShowDialog()
    End Sub

    Private Sub TextBoxChaineRecherchee_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxChaineRecherchee.TextChanged
        Me.Button13.Enabled = TextBoxChaineRecherchee.Text.Contains("$")
    End Sub

  
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        FormPreview.RichTextBox1.Text = ""
        For Each oMatch As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(Me.TextBoxRef.Text, Me.ComboBoxRegex.Text, System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
            Dim sRecherche As String = Me.TextBoxRapport.Text
            For i = 0 To oMatch.Groups.Count - 1
                sRecherche = sRecherche.Replace("$" & i, oMatch.Groups(i).Value)
            Next
            sRecherche = System.Text.RegularExpressions.Regex.Replace(sRecherche, "\$NB", "99", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
            'sRecherche = sRecherche.Replace("$NB", "99")
            FormPreview.RichTextBox1.AppendText(sRecherche & vbCrLf)
        Next
        FormPreview.ShowDialog()
    End Sub

    Private Sub Button16_Click(sender As System.Object, e As System.EventArgs) Handles Button16.Click
        FormRegEx.TextBoxRegEx.Text = Me.ComboBoxRegex.Text
        FormRegEx.RichTextBoxTest.Text = Me.TextBoxRef.Text
        If FormRegEx.ShowDialog = DialogResult.OK Then Me.ComboBoxRegex.Text = FormRegEx.TextBoxRegEx.Text
    End Sub
End Class